package org.ewicom.ppsaddressbook;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.ewicom.ppsaddressbook.model.Person;
import org.ewicom.ppsaddressbook.model.PersonDataSource;
import org.ewicom.ppsaddressbook.model.Phone;
import org.ewicom.ppsaddressbook.model.PhoneDataSource;
import org.ewicom.ppsaddressbook.model.Unit;
import org.ewicom.ppsaddressbook.model.UnitDataSource;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.util.Linkify;
import android.widget.LinearLayout;
import android.widget.TextView;

public class UnitDetails extends Activity {

	private static final String TAG = "UnitDetails";

	private UnitDataSource unitDataSource;
	private PersonDataSource personDataSource;
	private PhoneDataSource phoneDataSource;

	private long unitID;

	private TextView nameTextView;
	private TextView streetTextView;
	private TextView postcodeTextView;
	private TextView cityTextView;
	private TextView phoneTextView;
	private TextView emailTextView;

	private LinearLayout personLinearLayout;
	private LinearLayout phoneLinearLayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.unitdetails);

		final Intent intent = getIntent();
		unitID = intent.getLongExtra("unit_id", -1);

		setAddressDetails();
		setPersonDetails();
		setPhoneDetails();
	}

	private final void setAddressDetails() {
		nameTextView = (TextView) findViewById(R.id.unit_name);
		streetTextView = (TextView) findViewById(R.id.unit_street);
		postcodeTextView = (TextView) findViewById(R.id.unit_postcode);
		cityTextView = (TextView) findViewById(R.id.unit_city);
		phoneTextView = (TextView) findViewById(R.id.unit_phone);
		emailTextView = (TextView) findViewById(R.id.unit_email);

		unitDataSource = new UnitDataSource(this);
		unitDataSource.open();

		Unit unit = unitDataSource.getUnitById(unitID);

		nameTextView.setText(unit.getName());
		streetTextView.setText(unit.getStreet());
		postcodeTextView.setText(unit.getPostcode());
		cityTextView.setText(unit.getCity());
		phoneTextView.setText(unit.getPhone());
		emailTextView.setText(unit.getEmail());

		Linkify.addLinks(phoneTextView, Linkify.PHONE_NUMBERS);
		Linkify.addLinks(emailTextView, Linkify.EMAIL_ADDRESSES);

	}

	private final void setPersonDetails() {
		personLinearLayout = (LinearLayout) findViewById(R.id.unit_details_person);

		personDataSource = new PersonDataSource(this);
		personDataSource.open();

		List<Person> persons = new ArrayList<Person>();
		persons = personDataSource.getPersonsByUnitId(unitID);

		
		if(persons != null && !persons.isEmpty()){
		Iterator<Person> iterator = persons.iterator();

		while (iterator.hasNext()) {

			Person person = iterator.next();

			TextView positionTV = (TextView) getLayoutInflater().inflate(R.layout.person_position, null);
			TextView personTV = (TextView) getLayoutInflater().inflate(R.layout.person_person, null);
			TextView phoneTV = (TextView) getLayoutInflater().inflate(R.layout.person_phone, null);
			TextView emailTV = (TextView) getLayoutInflater().inflate(R.layout.person_email, null);

			positionTV.setText(person.getPosition());
			personTV.setText(person.getPerson());
			phoneTV.setText(person.getPhone());
			emailTV.setText(person.getEmail());

			Linkify.addLinks(phoneTV, Linkify.PHONE_NUMBERS);
			Linkify.addLinks(emailTV, Linkify.EMAIL_ADDRESSES);

			personLinearLayout.addView(positionTV);
			personLinearLayout.addView(personTV);
			personLinearLayout.addView(phoneTV);
			personLinearLayout.addView(emailTV);
		}}else{
			TextView emptyTV = (TextView) getLayoutInflater().inflate(R.layout.empty_details, null);
			emptyTV.setText(R.string.emptyPersonDetails);
			personLinearLayout.addView(emptyTV);
		}
		
	}

	private final void setPhoneDetails() {
		phoneLinearLayout = (LinearLayout) findViewById(R.id.unit_details_phones);

		phoneDataSource = new PhoneDataSource(this);
		phoneDataSource.open();

		List<Phone> phones = new ArrayList<Phone>();
		phones = phoneDataSource.getPhonesByUnitId(unitID);

		if (phones != null && !phones.isEmpty()) {
			Iterator<Phone> iterator = phones.iterator();

			while (iterator.hasNext()) {

				Phone phone = iterator.next();
				
				TextView phoneTypeTV = (TextView) getLayoutInflater().inflate(R.layout.phone_type_tv, null);
				TextView phoneTV = (TextView) getLayoutInflater().inflate(R.layout.phone, null);

				phoneTypeTV.setText(phone.getType());
				phoneTV.setText(phone.getPhone());

				Linkify.addLinks(phoneTypeTV, Linkify.PHONE_NUMBERS);
				Linkify.addLinks(phoneTV, Linkify.PHONE_NUMBERS);

				phoneLinearLayout.addView(phoneTypeTV);
				phoneLinearLayout.addView(phoneTV);
			}
		} else {
			TextView emptyTV = (TextView) getLayoutInflater().inflate(R.layout.empty_details, null);
			emptyTV.setText(R.string.emptyPhoneDetails);
			phoneLinearLayout.addView(emptyTV);
		}

	}

	@Override
	protected void onResume() {
		unitDataSource.open();
		personDataSource.open();
		phoneDataSource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		unitDataSource.close();
		personDataSource.close();
		phoneDataSource.close();
		super.onPause();
	}
}
